﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADNLib;

namespace ADN
{
	public partial class FormNew : Form
	{
        private ADNDocumentType docType = ADNDocumentType.Unknown;
		private bool changeBySelection = false;

		public FormNew()
		{
			InitializeComponent();
		}

        public decimal FPS
        {
            get { return spinFPS.Value; }
            set { spinFPS.Value = value; }
        }

        public decimal ResolutionHeight
        {
            get { return spinHeight.Value; }
            set { spinHeight.Value = value; }
        }

        public decimal ResolutionWidth
        {
            get { return spinWidth.Value; }
            set { spinWidth.Value = value; }
        }

        public Color BackColor
        {
            get { return btnBackColor.BackColor; }
            set { btnBackColor.BackColor = value; }
        }

        public ADNDocumentType DocumentType
        {
            get { return docType; }
            set { docType = value; }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

		private void lstDocuments_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstDocuments.SelectedItems.Count > 0)
			{
				ListViewItem lvi = lstDocuments.SelectedItems[0];
				SetOptions((string)lvi.Tag);
			}
		}

		//MUST CHANGE SelectByValue if these values change
		private void SetOptions(string selectedItem)
		{
			changeBySelection = true;

			switch (selectedItem)
			{
				case "4K_4:3":
					spinWidth.Value = 4096;
					spinHeight.Value = 3072;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "4K_16:9":
					spinWidth.Value = 4096;
					spinHeight.Value = 2304;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "4K_1.85:1":
					spinWidth.Value = 4096;
					spinHeight.Value = 2160;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "4K_2.39:1":
					spinWidth.Value = 4096;
					spinHeight.Value = 1716;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "SDDVD_4:3":
					spinWidth.Value = 720;
					spinHeight.Value = 480;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "SDDVD_16:9":
					spinWidth.Value = 640;
					spinHeight.Value = 480;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "HDDVD_4:3":
					spinWidth.Value = 1920;
					spinHeight.Value = 1080;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "HDDVD_16:9":
					spinWidth.Value = 1280;
					spinHeight.Value = 720;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "Custom":
					//spinWidth.Value = 4000;
					//spinHeight.Value = 3000;
					//spinFPS.Value = 24;
					docType = ADNDocumentType.Animation;
					break;
				case "Character":
					spinWidth.Value = 1000;
					spinHeight.Value = 750;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Character;
					break;
				case "SwapGroup":
					spinWidth.Value = 100;
					spinHeight.Value = 100;
					spinFPS.Value = 24;
					docType = ADNDocumentType.SwapGroup;
					break;
				case "Document":
					spinWidth.Value = 745;
					spinHeight.Value = 1052;
					spinFPS.Value = 24;
					docType = ADNDocumentType.Document;
					break;
			}

			changeBySelection = false;
		}

		private ListViewItem SelectByValue(int height, int width, string tag)
		{
			switch (tag)	//don't change the selected item if it's one of these types.
			{
				case "Custom": return null;
				case "Document": return null;
				case "Character": return null;
				case "SwapGroup": return null;
			}

			switch (width)
			{
				case 4096:
					switch (height)
					{
						case 3072: return lstDocuments.FindItemWithText("4K Cinema (4:3)");
						case 2304: return lstDocuments.FindItemWithText("4K Cinema (16:9)");
						case 2160: return lstDocuments.FindItemWithText("4K Cinema (1.85:1)");
						case 1716: return lstDocuments.FindItemWithText("4K Cinema (2.39:1)");
						default: return lstDocuments.FindItemWithText("Custom");
					}
				case 720:
					if (height == 480) { return lstDocuments.FindItemWithText("Standard Definition DVD (4:3)"); }
					else { return lstDocuments.FindItemWithText("Custom"); }
				case 640:
					if (height == 480) { return lstDocuments.FindItemWithText("Standard Definition DVD (16:9)"); }
					else { return lstDocuments.FindItemWithText("Custom"); }
				case 1920:
					if (height == 1080) { return lstDocuments.FindItemWithText("High Definition DVD (4:3)"); }
					else { return lstDocuments.FindItemWithText("Custom"); }
				case 1280:
					if (height == 720) { return lstDocuments.FindItemWithText("High Definition DVD (16:9)"); }
					else { return lstDocuments.FindItemWithText("Custom"); }
				default:
					return lstDocuments.FindItemWithText("Custom");
			}
		}

		private void spinFPS_ValueChanged(object sender, EventArgs e)
		{
			
		}

		private void spinWidth_ValueChanged(object sender, EventArgs e)
		{
			if (!changeBySelection)
			{
				string tag = String.Empty;
				if (lstDocuments.SelectedItems.Count > 0) { tag = (string)lstDocuments.SelectedItems[0].Tag; }
				ListViewItem lvi = SelectByValue((int)spinHeight.Value, (int)spinWidth.Value, tag);
				if (lvi != null) { lvi.Selected = true; }
			}
		}

		private void spinHeight_ValueChanged(object sender, EventArgs e)
		{
			if (!changeBySelection)
			{
				string tag = String.Empty;
				if (lstDocuments.SelectedItems.Count > 0) { tag = (string)lstDocuments.SelectedItems[0].Tag; }
				ListViewItem lvi = SelectByValue((int)spinHeight.Value, (int)spinWidth.Value, tag);
				if (lvi != null) { lvi.Selected = true; }
			}
		}

        private void lstDocuments_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewItem lvi = lstDocuments.GetItemAt(e.Location.X, e.Location.Y);

            if (lvi != null)
            {
                SetOptions((string)lvi.Tag);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
	}
}
